let createNewPromise = (url,data,method,promiseResolve)=>{
	console.log(getApp().globalData.token);
	return new Promise(() => {
		wx.request({
			url:url,
			data:data,
			method:method,
			header:{
				token: getApp().globalData.token
			},
			success:(res)=>{ 
				promiseResolve(res.data); 
			},
			fail:(err)=>{    
			  	console.log(err);    
			},
		})
	})
}

let getResult = (url,params,method)=>{
	let promiseTemp = null;
	let promiseResolve = null;

	async function fun(){
	    promiseTemp = new Promise(resolve => promiseResolve = resolve); 
		createNewPromise(url,params,method,promiseResolve);
		let res = await promiseTemp;
		return res;
	}
	return fun()
}

//获取课程列表 kind类型 current分页
const getCourseList = (kind,current)=>{
	let url = getApp().globalData.baseUrl + "course";
	let params = {
		size: getApp().globalData.size,
		kind: kind,
		current: current
	}
	return getResult(url,params,"GET");
}

//获取课程内容的详情 courseId课程id
const getCourseDetail = (courseId)=>{
	let url = getApp().globalData.baseUrl + "course/detail";
	let params = {
		courseId: courseId
	}
	return getResult(url,params,"GET");
}

//获取课程章节列表 courseId课程id
const getCourseSection = (courseId)=>{
	let url = getApp().globalData.baseUrl + "course/section";
	let params = {
		courseId: courseId,
		
	}
	return getResult(url,params,"GET");
}

//加入课程
const joinCourse = (courseId)=>{
	let url = getApp().globalData.baseUrl + "course/join";
	let data ={
		courseId: courseId
	};
	// wx.request({
	//   	url: url,
	// 	method: "POST",
	// 	data :{
	// 		courseId: courseId
	// 	},
	// 	header:{
	// 		token: getApp().globalData.token
	// 	},
	// 	success:(res)=>{
	// 		console.log(res);
	// 	}
	// })
	return getResult(url,data,"POST");
	// return getResult(url,data,"POST");
}

const getMyCourse = ()=>{
	let url = getApp().globalData.baseUrl + "course/my";
	let params = {
	}
	return getResult(url,params,"GET");
}

export default{
	getCourseList,
	getCourseDetail,
	getCourseSection,
	joinCourse,
	getMyCourse
}